let api = {
    "pswLogin": '/api/pswLogin',
    "emailLogin": '/api/emailLogin',
    "sendEmail": '/api/sendEmail',
    "userRegister": '/api/userRegister',
    "modifyName": '/api/modifyName',
    "modifyPsw": '/api/modifyPsw'
}
let route = {
    "emailLogin": "/view/other/emailLogin.html",
    "pswLogin": "/view/other/pswLogin.html",
    "register": "/view/other/register.html",
    "userInfo": "/view/other/userInfo.html",
    "modifyName": "/view/other/modifyName.html",
    "modifyPsw": "/view/other/modifyPsw.html",
    "noviceGuidance": "/view/other/noviceGuidance.html",
    "productIntro": "/view/other/productIntro.html",
}

/*弹窗封装*/
function alertBySweet(text) {
    let alertDiv = document.createElement("div");
    let alertButton = document.createElement("button");
    alertButton.innerHTML = text;
    alertDiv.id = "alertDiv";
    document.body.appendChild(alertDiv);
    alertDiv.appendChild(alertButton);
    let style = "#alertDiv{\n" +
        "            position: absolute;\n" +
        "            left: 0;\n" +
        "            right: 0;\n" +
        "            top: 0;\n" +
        "            bottom: 0;\n" +
        "            margin: auto;\n" +
        "        }\n" +
        "        #alertDiv button{\n" +
        "            position: absolute;\n" +
        "            left: 0;\n" +
        "            right: 0;\n" +
        "            top: 0;\n" +
        "            bottom: 0;\n" +
        "            margin: auto;\n" +
        "            height: 2rem;\n" +
        "            padding: 0 8px;\n" +
        "            background-color: black;\n" +
        "            opacity: 0.8;\n" +
        "            text-align: center;\n" +
        "            color: white;\n" +
        "            border: none;\n" +
        "            border-radius: 5px;\n" +
        "            animation:animate 1s;\n" +
        "        }\n" +
        "\n" +
        "        @keyframes animate{\n" +
        "            from{top:10%;opacity:0}\n" +
        "            to{top:0%;opacity:1}\n" +
        "        }";
    let styleNode = document.createElement("style");
    styleNode.id = "alertStyle";
    styleNode.setAttribute("type", "text/css");
    styleNode.innerHTML = style;
    document.body.appendChild(styleNode);
    setTimeout(closeAlert, 3000);
}

/*带确定按钮的弹窗*/
function alertWithButtonBySweet(text) {
    let alertDiv = document.createElement("div");
    let alertP = document.createElement("p");
    let buttonDiv = document.createElement("div");
    let alertButton = document.createElement("button");
    alertP.innerHTML = text;
    alertButton.innerHTML = "确定";
    alertDiv.id = "alertDiv";
    buttonDiv.id = "buttonDiv";
    alertButton.id = "confirmButton";
    document.body.appendChild(alertDiv);
    alertDiv.appendChild(alertP);
    alertDiv.appendChild(buttonDiv);
    buttonDiv.appendChild(alertButton);
    alertButton.addEventListener("click", closeAlert);
    let pattern = new RegExp("[\u4E00-\u9FA5]+");// 中文字符
    let textLength = 0;
    // 需要分别统计中文和英文的长度，英文两个单词算一个
    for (let i = 0; i < text.length; i++) {
        if (pattern.test(text.charAt(i))) {
            textLength += 2;
        } else {
            textLength += 1;
        }
    }
    console.log(textLength);
    let height = 0;
    if (textLength % 36 != 0) {
        if ((textLength / 36) == 0) {
            height = (textLength / 36) * 1 + 4;
        } else {
            height = (textLength / 36) * 1 + 5;
        }
    } else {
        height = (textLength / 36) * 1 + 4;
    }
    let style = "#alertDiv {\n" +
        "            position: absolute;\n" +
        "            left: 0;\n" +
        "            right: 0;\n" +
        "            top: 0;\n" +
        "            bottom: 0;\n" +
        "            margin: auto;\n" +
        "            width: 300px;\n" +
        "            height: " + height + "rem;\n" +
        "            padding: 0.5rem;\n" +
        "            background-color: black;\n" +
        "            border-radius: 5px;\n" +
        "            opacity: 0.8;\n" +
        "            animation: animate 1s;\n" +
        "        }\n" +
        "\n" +
        "        #alertDiv p {\n" +
        "            word-break: break-word;\n" +
        "            line-height: 1rem;\n" +
        "            color: white;\n" +
        "            font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace;\n" +
        "        }\n" +
        "\n" +
        "        #buttonDiv{\n" +
        "            position: relative;\n" +
        "            height: 3rem;\n" +
        "            width: 100%;\n" +
        "            text-align: right;\n" +
        "        }\n" +
        "\n" +
        "        #alertDiv button {\n" +
        "            height: 2rem;\n" +
        "            line-height: 2rem;\n" +
        "            padding: 0 1rem;\n" +
        "            background-color: #4788f4;\n" +
        "            text-align: center;\n" +
        "            color: white;\n" +
        "            border: none;\n" +
        "            border-radius: 5px;\n" +
        "            outLine: none;\n" +
        "        }\n" +
        "\n" +
        "        @keyframes animate {\n" +
        "            from {\n" +
        "                top: 10%;\n" +
        "                opacity: 0\n" +
        "            }\n" +
        "            to {\n" +
        "                top: 0%;\n" +
        "                opacity: 1\n" +
        "            }\n" +
        "        }";
    let styleNode = document.createElement("style");
    styleNode.id = "alertStyle";
    styleNode.setAttribute("type", "text/css");
    styleNode.innerHTML = style;
    document.body.appendChild(styleNode);
}

/*带输入框的弹窗*/
function alertWithInputBySweet() {
    let alertDiv = document.createElement("div");
    let alertInput = document.createElement("input");
    let buttonDiv = document.createElement("div");
    let sureButton = document.createElement("button");
    let cancelButton = document.createElement("button");
    alertInput.placeholder = "请输入...";
    sureButton.innerHTML = "确定";
    cancelButton.innerHTML = "取消";
    alertInput.id = "alertInput";
    alertDiv.id = "alertDiv";
    sureButton.id = "sureButton";
    cancelButton.id = "cancelButton";
    document.body.appendChild(alertDiv);
    alertDiv.appendChild(alertInput);
    alertDiv.appendChild(buttonDiv);
    buttonDiv.appendChild(cancelButton);
    buttonDiv.appendChild(sureButton);
    sureButton.addEventListener("click", returnMessage);
    cancelButton.addEventListener("click", closeAlert);
    let style = "#alertDiv {\n" +
        "            position: absolute;\n" +
        "            left: 0;\n" +
        "            right: 0;\n" +
        "            top: 0;\n" +
        "            bottom: 0;\n" +
        "            margin: auto;\n" +
        "            width: 380px;\n" +
        "            padding: 20px 10px 20px 10px;\n" +
        "            height: 5rem;\n" +
        "            background-color: black;\n" +
        "            border-radius: 5px;\n" +
        "            opacity: 0.8;\n" +
        "            animation: animate 1s;\n" +
        "        }\n" +
        "\n" +
        "        #alertDiv input {\n" +
        "            width: 375px;\n" +
        "            outline: none;\n" +
        "            line-height: 2rem;\n" +
        "            border: none;\n" +
        "            margin-bottom: 1rem;\n" +
        "        }\n" +
        "\n" +
        "        #alertDiv div{\n" +
        "            position: relative;\n" +
        "            height: 2rem;\n" +
        "            width: 100%;\n" +
        "            text-align: right;\n" +
        "        }\n" +
        "\n" +
        "        #alertDiv button {\n" +
        "            height: 2rem;\n" +
        "            line-height: 2rem;\n" +
        "            padding: 0 1rem;\n" +
        "            background-color: #4788f4;\n" +
        "            text-align: center;\n" +
        "            color: white;\n" +
        "            border: none;\n" +
        "            border-radius: 5px;\n" +
        "            display: flex;\n" +
        "            float: right;\n" +
        "            margin-left: 5px;\n" +
        "            outline: none;\n" +
        "        }\n" +
        "\n" +
        "        @keyframes animate {\n" +
        "            from {\n" +
        "                top: 10%;\n" +
        "                opacity: 0\n" +
        "            }\n" +
        "            to {\n" +
        "                top: 0%;\n" +
        "                opacity: 1\n" +
        "            }\n" +
        "        }";
    let styleNode = document.createElement("style");
    styleNode.id = "alertStyle";
    styleNode.setAttribute("type", "text/css");
    styleNode.innerHTML = style;
    document.body.appendChild(styleNode);
}

/*返回输入结果*/
function returnMessage() {
    let value = document.getElementById("alertInput").value;
    closeAlert();
    return value;
}

/*移除弹窗*/
function closeAlert() {
    let alertDialog = document.getElementById("alertDiv");
    let alertStyle = document.getElementById("alertStyle");
    alertDialog.style.display = "none";
    document.body.removeChild(alertDialog);
    document.body.removeChild(alertStyle);
}
